package com.wjpeng.wms.service.impl;

import com.wjpeng.wms.domain.Department;
import com.wjpeng.wms.mapper.DepartmentMapper;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IDepartmentService;
import com.wjpeng.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void saveOrUpdate(Department d) {
        if(d.getId() == null){
            departmentMapper.insert(d);
        }else{
            departmentMapper.updateByPrimaryKey(d);
        }
    }

    @Override
    public void delete(Long id) {
            departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> list() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageResult result(QueryObject qo) {
        Integer count = departmentMapper.queryCount(qo);
        if(count == 0){
            return new PageResult(Collections.EMPTY_LIST,0,1,0);
        }
        return new PageResult(departmentMapper.queryLimit(qo),count,qo.getCurrentPage(),qo.getPageSize());
    }
}
